package keyboard.model;

import java.util.Observable;

public class Keyboard extends Observable {
	
	private StringBuilder username;
	private StringBuilder password;
	private StringBuilder active;
	private boolean caps;
	private boolean shift;
	private Login model;
	
	public Keyboard(Login model) {
		this.model = model;
		username = new StringBuilder();
		password = new StringBuilder();
		active = username;
	}
	
	public boolean isCaps() {
		return caps || shift;
	}
	
	public void backspace() {
		if (active.length() != 0)
			active.deleteCharAt(active.length()-1);
	}
	
	public void enter() {
		if (model.login(username.toString(), password.toString())) {
			
		}
	}
	
	public void pressKey(String key) {
		active.append(key);
		if (shift = true) {
			shift = false;
			setChanged();
			notifyObservers();
		}
		
	}
	
	public void toggleCaps() {
		caps = !caps;
		shift = false;
		setChanged();
		notifyObservers();
	}
	
	public void tab() {
		if (active == username)
			active = password;
		else
			active = username;
	}
	
	public void toggleShift() {
		shift = !shift;
		setChanged();
		notifyObservers();
	}
	
	public String getUsername() {
		return username.toString();
	}
	
	public String getPassword() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < password.length(); i++)
			sb.append("*");
		return sb.toString();
	}
	
	
}
